package com.work.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.work.domain.CompanyInfo;
import com.work.domain.UserAdmin;
import com.work.domain.UserLoginlog;
import com.work.service.UserLogService;
import com.work.util.DateUtil;
import com.work.util.GetLoginLog;
import com.work.util.GetUserSessionTool;

import net.sf.json.JSONArray;

/**
 * 
 * @author zhubingyi
 * @version 1.0
 * 
 */
@Controller
@RequestMapping("/UserLog")
public class UserLogController {
	
	@Resource
	private UserLogService userLogService;
	
	//登陆日志列表页面
	@RequestMapping(value="loginLogList",produces="text/html;charset=UTF-8")
	public String loginLogList(Model model,HttpServletRequest request) throws IOException{
		request.setCharacterEncoding("UTF-8");
		//验证session是否过期
		UserAdmin useradmin = GetUserSessionTool.getUserSession(request);
		if(useradmin==null||"".equals(useradmin)){
			model.addAttribute("ngworkusererror", "用户登录已超时，请重新登录！");
			return "fail";
		}else{
			List<UserLoginlog> list=userLogService.findAllLoginLogInfo();
			JSONArray jsonArray = JSONArray.fromObject(list);
			model.addAttribute("loginLogList", jsonArray);
		    return "userLog/loginLogList";
		}
	}
	//删除日志信息
	@RequestMapping("deleteUserLog")
	public void deleteUserLog(Model model,HttpServletRequest request,HttpServletResponse response){
		String ids = request.getParameter("ids");
		JSONArray fo = JSONArray.fromObject(ids);
		int flag=0;
		PrintWriter writer = null;
		Integer deleteInfo=null;
		//验证session是否过期
		UserAdmin useradmin = GetUserSessionTool.getUserSession(request);
		if(useradmin==null||"".equals(useradmin)){
			writer.print("用户登录已超时，请重新登录！");
			writer.flush();
			writer.close();
		}else{
			try {
			    for (Object object : fo) {
					writer = response.getWriter();
					flag++;
					int id = Integer.parseInt(object.toString());
					deleteInfo= userLogService.deleteLoginLogById(id);
				   }
				if(flag==fo.size()){
						if(deleteInfo==1){
							writer.print("1");
							writer.flush();
						}
					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} finally {
					writer.close();
				}
		}
	} 
	//根据条件查询企业列表信息
	@RequestMapping(value="userLogListByConditions",produces="text/html;charset=UTF-8")
	public String userLogListByConditions(Model model,UserLoginlog userLoginlog,HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException{
			response.setContentType("text/plain; charset=UTF-8");
			request.setCharacterEncoding("UTF-8");
			String percent="%";
			String usernamelog = userLoginlog.getUsernamelog();
			String userrolelog = userLoginlog.getUserrolelog();
			String loginiplog = userLoginlog.getLoginiplog();
			String logintimelog = userLoginlog.getLogintimelog();
			//用户名
			usernamelog=percent.concat(usernamelog).concat(percent);
			//用户角色
			userrolelog =percent.concat(userrolelog).concat(percent);
			//用户ip
			loginiplog =percent.concat(loginiplog).concat(percent);
			//登陆时间
			logintimelog =percent.concat(logintimelog).concat(percent);
			//验证session是否过期
			UserAdmin useradmin = GetUserSessionTool.getUserSession(request);
			if(useradmin==null||"".equals(useradmin)){
				model.addAttribute("ngworkusererror", "用户登录已超时，请重新登录！");
				return "fail";
			}else{
				List<UserLoginlog> list=userLogService.findUserLogListByConditions(usernamelog,userrolelog,loginiplog,logintimelog);
				JSONArray jsonArray = JSONArray.fromObject(list);
				model.addAttribute("loginLogList", jsonArray);
			    return "userLog/loginLogList";
			}
		}
}
